package com.bayihouse.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bayihouse.annotation.AuthCheck;
import com.bayihouse.common.BaseResponse;
import com.bayihouse.common.DeleteRequest;
import com.bayihouse.common.ErrorCode;
import com.bayihouse.common.ResultUtils;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.constant.AdministerConstant;
import com.bayihouse.exception.BusinessException;
import com.bayihouse.exception.ThrowUtils;
import com.bayihouse.model.entity.Occupant;
import com.bayihouse.model.entity.OutPerson;
import com.bayihouse.model.request.occupant.OccupantUpdateRequest;
import com.bayihouse.model.request.outperson.*;
import com.bayihouse.model.vo.OutPersonVO;
import com.bayihouse.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;


@RestController
@RequestMapping("/outperson")
@Slf4j
public class OutPersonController {


    @Resource
    private OutPersonService outpersonService;


    // endregion

    // region 增删改查

    /**
     * 创建用户
     *
     * @param outpersonAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Long> addOutPerson(@RequestBody OutpersonAddRequest outpersonAddRequest, HttpServletRequest request) {
        if (outpersonAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        OutPerson outperson = new OutPerson();
        BeanUtils.copyProperties(outpersonAddRequest, outperson);
        boolean result = outpersonService.save(outperson);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(outperson.getId());
    }

    @PostMapping("/update")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> updateOutperson(@RequestBody OutpersonUpdateRequest outpersonUpdateRequest,
                                                 HttpServletRequest request) {
        if (outpersonUpdateRequest == null || outpersonUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        OutPerson outPerson = new OutPerson();
        BeanUtils.copyProperties(outpersonUpdateRequest, outPerson);
        boolean result = outpersonService.updateById(outPerson);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 删除用户
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Boolean> deleteOutPerson(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = outpersonService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }


    /**
     * 根据 id 获取用户（仅管理员）
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<OutPersonVO> getOutPersonById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        OutPerson outperson = outpersonService.getById(id);
        ThrowUtils.throwIf(outperson == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(OutPersonVO.objToVo(outperson));
    }

    public BaseResponse<OutPerson> getOutPersonById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        OutPerson outperson = outpersonService.getById(id);
        ThrowUtils.throwIf(outperson == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(outperson);
    }

    /**
     * 根据 id 获取包装类
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<OutPersonVO> getOutPersonVOById(long id, HttpServletRequest request) {
        BaseResponse<OutPerson> response = getOutPersonById(id);
        OutPerson outperson = response.getData();
        return ResultUtils.success(outpersonService.getOutPersonVO(outperson));
    }

    /**
     * 分页获取用户列表（仅管理员）
     *
     * @param outpersonQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<OutPerson>> listOutPersonByPage(@RequestBody OutPersonQueryRequest outpersonQueryRequest,
                                                             HttpServletRequest request) {
        long current = outpersonQueryRequest.getCurrent();
        long size = outpersonQueryRequest.getPageSize();
        Page<OutPerson> outpersonPage = outpersonService.page(new Page<>(current, size),
                outpersonService.getQueryWrapper(outpersonQueryRequest));
        return ResultUtils.success(outpersonPage);
    }

    /**
     * 分页获取用户封装列表
     *
     * @param outpersonQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    @AuthCheck(mustRole = AdministerConstant.DEFAULT_ROLE)
    public BaseResponse<Page<OutPersonVO>> listOutPersonVOByPage(@RequestBody OutPersonQueryRequest outpersonQueryRequest,
                                                                 HttpServletRequest request) {
        if (outpersonQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        long current = outpersonQueryRequest.getCurrent();
        long size = outpersonQueryRequest.getPageSize();
        System.out.println(outpersonQueryRequest);
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<OutPerson> outpersonPage = outpersonService.page(new Page<>(current, size),
                outpersonService.getQueryWrapper(outpersonQueryRequest));
        System.out.println(outpersonPage.getRecords());
        Page<OutPersonVO> outpersonVOPage = new Page<>(current, size, outpersonPage.getTotal());
        List<OutPersonVO> outpersonVO = outpersonService.getOutPersonVOList(outpersonPage.getRecords());
        outpersonVOPage.setRecords(outpersonVO);
        System.out.println(outpersonVOPage.getRecords());
        return ResultUtils.success(outpersonVOPage);
    }


}

